import type { ModelStuck } from '@/types'
import * as templateComp from '@/template'
import { useRef } from 'react'

export enum CodeTypes {
  CREATE = 'create',
  UPDATE = 'update',
  DELETE = 'delete',
  QUERY = 'query'
}

export enum FontendTypes {
  INDEX = 'index',
  DATA = 'data',
  FORM = 'form'
}

/**
 * useTemplate:生成模版代码
 */
export const useTemplate = (type: string) => {
  const typeRef = useRef(type)
  // 通用白名单字段
  const whiteKeyList = ['createTime', 'updateTime', 'delState', 'deleteTime', 'version']
  // 生成proto描述文件
  const parseToRrotoOrApi = (options: ModelStuck) => {
    return type == 'rpc'
      ? templateComp.getProtoString(options, whiteKeyList)
      : templateComp.getApiDescString(options, whiteKeyList)
  }
  // 生成增删改查代码
  const parseToProtoOrApiCode = (options: ModelStuck, type: CodeTypes) => {
    switch (type) {
      case CodeTypes.CREATE:
        return typeRef.current == 'rpc'
          ? templateComp.getRpcCreateString(options)
          : templateComp.getApiCreateString(options)
      case CodeTypes.DELETE:
        return typeRef.current == 'rpc'
          ? templateComp.getRpcDeleteString(options)
          : templateComp.getApiDeleteString(options)
      case CodeTypes.QUERY:
        return typeRef.current == 'rpc'
          ? templateComp.getRpcQueryString(options, whiteKeyList)
          : templateComp.getApiQueryString(options)
      case CodeTypes.UPDATE:
        return typeRef.current == 'rpc'
          ? templateComp.getRpcUpdateString(options)
          : templateComp.getApiUpdateString(options)
      default:
        return ''
    }
  }

  // 生成前端代码
  const parseToFontendCode = (options: ModelStuck, type: FontendTypes) => {
    switch (type) {
      case FontendTypes.INDEX:
        return templateComp.getFontendIndexCodeString(options)
      case FontendTypes.DATA:
        return templateComp.getFontendDataCodeString(options, whiteKeyList)
      case FontendTypes.FORM:
        return templateComp.getFontendDataFormString(options, whiteKeyList)
      default:
        return ''
    }
  }

  return {
    whiteKeyList,
    parseToRrotoOrApi,
    parseToProtoOrApiCode,
    parseToFontendCode
  }
}
